0

I'm trying to do this:

domain.com/s/united-states
domain.com/s/singapore
domain.com/s/anything

will redirect to the page of domain.com/s/, but keeps the original URL which was:

domain.com/s/united-states
domain.com/s/singapore
domain.com/s/anything

The reason I'm doing this is because I am building a search page at domain.com/s/ and using the endpoint of the URL as the search term. I tried the following:

RewriteRule ^s/([a-zA-Z_-]+)$ /s/ [NC,NE,R,L]

which works but does not keep the original URL.

I tried:

RewriteRule ^s/([a-zA-Z_-]+)$ /s/ [NC,NE,L] 

which does not work. May I have your thoughts about it?

The redirect also needs to keep any get parameter at the back like example:

domain.com/s/primary-search-term?filter1=x&filter2=y

This kind of URL handling can be seen in pages like Airbnb:

https://www.airbnb.com/s/Singapore?guests=1&adults=1&children=0&infants=0&ss_id=th87ej7h&source=bb

A snippet of my .htaccess is as below:

## No directory listings
IndexIgnore *

## Can be commented out if causes errors, see notes above.
Options +FollowSymlinks
Options -Indexes

## Mod_rewrite in use.

RewriteEngine On
RewriteRule ^s/([a-zA-Z_-]+)$ /s/ [NC,NE,L]

In case it means anything, this is part of the Joomla default .htaccess with SH404SEF installed.

UPDATE: I tried the following which worked on one test server but failed on production...

RewriteRule ^d/([a-zA-Z_-]+)$ /index.php?option=com_quix&Itemid=876&id=105&lang=en&view=page&fulladdress=$1 [L,QSA]

The output on the access log showed

[07/Feb/2017:08:42:57 +0000] "GET /d/singapore HTTP/1.1" 404 24732 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36" www.domain.com:443 124.82.84.207

Everything seems the same.. no idea why it would fail.

Chizuoka
  • 57
  • 1
  • 10

1 Answers1

0

You need to drop the R flag as it's designed for external redirects and this does not preserve the original URL. What you need is an internal redirect.

You also need to use QSA flag to keep the query string, if needed. It's there by default. So:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^s/([a-zA-Z_-]+)$ /s/ [L]
</IfModule>

But there's probably a problem here; you're missing the passed data (e.g. united-states). You probably want that to be mapped to your PHP script. To pass that as a querystring, you can do something like this:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^s/([a-zA-Z_-]+)$ /s/?country=$1 [L,QSA]
</IfModule>

This way you're merging a new query string parameter (country) with all existing QS parameters, if any.

One more thing; you won't need a IndexIgnore * directive with Options -Indexes.

Community
  • 1
  • 1
sepehr
  • 17,110
  • 7
  • 81
  • 119
  • Thanks so much for the super quick reply. I tried it without the R RewriteRule ^s/([a-zA-Z_-]+)$ /s/ [NC,NE,L] but it fails to redirect totally and goes to 404 Is there something else that i missed out? – Chizuoka Jan 14 '17 at 09:09
  • Where's your htaccess file located? In the document root or in the `s/` directory? What do you get if you open `s/` in your browser? – sepehr Jan 14 '17 at 09:21
  • Its located in the root domain.com/. If i open s/ it shows the search page. I tried your suggestion... (RewriteRule ^s/([a-zA-Z_-]+)$ /s/?country=$1 [L,QSA] ) but it still returned a 404 – Chizuoka Jan 14 '17 at 09:30
  • I also removed the indexignore.. but still no luck – Chizuoka Jan 14 '17 at 09:36
  • It works on a vanilla 2.4 installation of Apache here. There are two possibilities; either the request does not get matched, or the destination throws a 404. You need to check the logs to see what's exactly happening. What's your apache version? – sepehr Jan 14 '17 at 09:37
  • See [How to debug apache mod_rewrite?](https://stackoverflow.com/questions/9632852/) – sepehr Jan 14 '17 at 09:46
  • Is `s/` mapped to an actual directory on the filesystem? or is it just a Joomla url? – sepehr Jan 14 '17 at 09:50
  • I assume it's a Joomla url. The 404 you're getting is because that the request gets rewritten to Joomla's index.php (after the custom rule you added), the url is not known to Joomla and thus the 404. You gonna need some Joomla advise. Maybe rewriting the request to `index.php` with page id instead of `/s/` works. – sepehr Jan 14 '17 at 10:01
  • Yes.. its going to index.php.. index.php?option=com_content&Itemid=880&lang=en&view=featured like that..... I change the htaccess entry to this RewriteRule ^s/([a-zA-Z_-]+)$ /index.php?option=com_content&Itemid=880&lang=en&view=featured/?country=$1 [L,QSA] turned on log.. and got a massive amount of log i dont even know where to start debugging from. Is my above entry right? – Chizuoka Jan 15 '17 at 16:31
  • I tried http://htaccess.madewithlove.be/ ... and ran domain.com/s/xxxx?param=2 with RewriteRule ^s/([a-zA-Z_-]+)$ /index.php?option=com_content&Itemid=880&lang=en&view=featured/?country=$1 [L,QSA] but it return that the rule was not met. – Chizuoka Jan 15 '17 at 16:57
  • No, it's not correct. Try this instead: `RewriteRule ^s/([a-zA-Z_-]+)$ /index.php?option=com_content&Itemid=880&lang=en&view=featur‌​ed&country=$1 [L,QSA]` – sepehr Jan 15 '17 at 18:50
  • In case you need to end the next rewrite rules, you can add an `END` flag to the end of the rewrite rule. – sepehr Jan 15 '17 at 18:53
  • I tried this RewriteRule ^d/([a-zA-Z_-]+)$ /index.php?option=com_quix&Itemid=876&id=105&lang=en&view=page&fulladdress=$1 [L,QSA]... it worked on one of my staging servers, but bringing it to production it throws a 404... any idea what else could prevent it running? – Chizuoka Feb 07 '17 at 08:51
  • Just to revisit this... i used `RewriteRule ^d/([a-zA-Z_-]+)$ /index.php?option=com_destinationlisting&view=default&fulladdress=$1 [L,QSA]` to get it to work – Chizuoka Apr 30 '17 at 05:05