I need a little help on this one.
I'm in the process of making a php page that collects a query string, tests it against a database for matches, and then redirects the user to a different section of the site.
The is how it works currently (without htaccess/mod_rewrite):
User visits: domain.com/redirect/index.php?slug=Test_1
The php page sanitizes and looks up 'Test_1' in the database and retrieves a destination URL to redirect the user to. (e.g. domain.com/New_Test_1)
The php page then 301 redirects accordingly.
This part is working fine. However, due to some variables outside of my control, I need to interpret the original URL (using htaccess/mod_rewrite), like this:
domain.com/redirect/index.php/Test_1
Which still acts the same as:
domain.com/redirect/index.php?slug=Test_1
(note: yes, the index.php needs to stay in the url.)
I have this working with the following in my htaccess, but I know it could be better:
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ %{DOCUMENT_ROOT}/redirect/index.php?slug=$2 [PT,L,QSA]
Part I need help with...
Some of the old url slugs had forward slashes in them, like this:
domain.com/redirect/index.php?slug=How_to_Code/Program
Without htaccess, the above still works, but fails with the pretty(ier) url:
domain.com/redirect/index.php/How_to_Code/Program
With my current htaccess, it only captures the 'How_to_Code' part, but ignores everything after it.
So my question is this: how can I restructure my htaccess to grab everything after domain.com/redirect/index.php/(.*)$, including forward slashes?
Edit: this .htaccess is going inside the /redirect directory
If you do this
RewriteRule ^redirect/index.php/([a-zA-Z0-9/_]+)$ redirect/index.php?slug=$1
That will accept forward slashes and underscores. To add anymore allowed characters just add them inside the [] square brackets. Certain characters may need to be escaped so just Google that.
Related
having issues redirecting from an existing page to another page. I have the URL:
http://www.thedomain.com/webpage-themission.php?wp=The%20Mission
and I want to redirect it to:
http://www.thedomain.com/webpage.php?wp=The%20Mission
I have tried:
RewriteEngine On
RewriteRule ^/webpage-themission.php?wp=The%20Mission$ http://thedomain.com/webpage.php?wp=The%20Mission$1 [L,R=301]
and many other combinations but no such luck, any help would be greatly appreciated!
You should not include the query string in the redirect rule, as it is not evaluated at all.
Try this:
RewriteEngine On
RewriteRule ^/?webpage-themission\.php$ http://thedomain.com/webpage.php [L,R=301,QSA]
A few notes:
It is important to note the QSA flag that is specified. This will have the effect of appending the query string wp=The%20Mission to the redirected URL.
I also escaped the . in the matching rule so that it does not behave as a wildcard and allow a redirect on something like /webpage-themissionXphp
I put the ? after the initial backslash, as typically in directory context (i.e. .htaccess) the forward slash is not compared, meaning that the rule would need to be ^webpage.... Putting /? makes the rule work in both the directory context and the server context (i.e. httpd.conf).
Please add this to your .htaccess file
Redirect 301 http://www.thedomain.com/webpage-themission.php?wp=The%20Mission http://www.thedomain.com/webpage.php?wp=The%20Mission
I have a php website with a docs folder in the root, the structure of the site is thus:
sitename.com/docs/
In the docs folder are PDF's that sometimes cause browser issues because of spaces in the names, so they are not found (mainly by IE).
What I would like to do is the following:
Whenever there is a broken URL in the docs/ directory, I would like to redirect the entire query string to a php page within the docs directory, but it must keep the name of the PDF intact.
Thus, is the URL is:
website.com/docs/this is a pdf
I want it to redirect to:
website.com/docs/index.php?pdf=this is a pdf
From there, I can grab the PDF param and fix it up and send the request to the correct file.
The reason this is not done with straight .htaccess is that I cannot find a solution that is dynamic, in other words the number of words in the PDF is variable, and could be from 1 to 20 words, separated by spaces.
I had a post up here about that at this SO post which did get one reply, however, it still does not address the variable URL length problem.
I have again tried this from the examples in this tutorial but this has not helped me at all as I cannot fathom how to do this properly.
The one thing that I think is close is the following code:
RewriteEngine on
RewriteRule ^docs/(.*) /index.php?pdf=$1 [NC]
Am I close?
First you should know that, only static codes can be written in .htaccess, and we cannot process to a dynamic code,The Following solution might help for you am not testedRewriteEngine onRewriteRule ^docs/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ /index.php?pdf=$1 [L]
Have your rule like this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^docs/((?=[^\s]*\s).+?)/?$ /docs/index.php?pdf=$1 [NC,L,B,QSA]
This will forward all PDFs with spaces to /docs/index.php while leaving non space file names intact.
I need all subdomains to be redirected to a specific page, without actually changing the URL, because I will display different content on this specific page depending on what subdomain is in the URL.
Let's say my site is located at testdomain.com/site1/
I want all subdomains, like xyz.testdomain.com/site1/ or even xyz.testdomain.com to be redirected to a specific page at http://testdomain.com/site1/index.php/test.php
The browser will then need to be loading http://testdomain.com/site1/index.php/test.php, but the URL will still be xyz.testdomain.com.
The purpose of this is so that someone can go to abc.testdomain.com or xyz.testdomain.com and both will take the user to testdomain.com/site1/index.php/test.php, and then on test.php, I have some code that will grab the URL, and if the url is abc.testdomain.com, it will display certain content, whereas if the subdomain is xyz.testdomain.com it will display different content.
Is this something I can do in htaccess? If so, how?
Using mod_rewrite you can hack this together.
# Step 1: If the user went to example.com or www.example.com
# then we don't want to redirect them. (S=1 says skip the next rule)
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule ^ - [S=1]
# Step 2: Anything else is redirected to our catcher script.
# Option 1: keeps the path they went to, but discards the domain
# i.e. xyz.example.com/abc/def.txt => /var/www/cgi-bin/abc/def.txt
RewriteRule ^/?(.*) /var/www/cgi-bin/$1 [QSA,L]
# Or Option 2: take all requests to the same file
# i.e. xyz.example.com/abc/def.txt => /var/www/cgi-bin/myfile.php
RewriteRule ^ /var/www/cgi-bin/myfile.php [QSA,L]
QSA tells it to forward the query string, L tells it to stop looking for more redirects (not absolutely necessary but sometimes helps if you have a lot of this sort of thing going on).
You can also pass variables to your script as query parameters, and the QSA flag ensures they don't replace the original values;
# xyz.example.com/abc/def.txt => /var/www/cgi-bin/myfile.php?host=xyz.example.com&path=/abc/def.txt
RewriteRule ^/?(.*) /var/www/cgi-bin/myfile.php?host=%{HTTP_HOST}&path=/$1 [QSA,L]
It means you don't need to worry about figuring out where the request came from inside your script (which might actually be impossible, I'm not sure). Instead you can just read it as a normal parameter (it's hackable like a normal parameter too; be sure to sanitise it).
I am trying to use mod_rewrite to redirect users keeping the multiple query string and creating a redirect page
For Example,
If user opens
http://localhost/url/url/http://www.google.com/contacts/?user=abc&stackoverflow=great&google=facebook
then he is taken to
http://localhost/url/url.php?redirect=http://www.google.com/contacts/?user=abc&stackoverflow=great&google=facebook
There is secondary problem that URL should be encoded and then redirected! If URL is not encoded then the string (&stackoverflow=great)would be not a part of 'redirect' string of url.php
I tried many solutions then came for stackoverflow! I tried the following code in following file
http://localhost/url/.htaccess
RewriteRule ^url/([^/])$ url.php?redirect=$1 [QSA,L]
but the result is localhost/url/url.php?redirect=http only
Your setup won't work with the unencoded inner url, so an 'answer' can only have temporary character. But this might be a starting point:
RewriteEngine on
RewriteRule ^/url/url/(.*)$ /url/url.php?redirect=$1 [L,QSA]
I wonder however if that fragment /url/url is really intended (the two 'url's in there).
Note that the exact rule content also depends on where you want to define that rule. The syntax is different whether you use the central server configuration (referred) or .htaccess style files (as second choice and more complex).
Try this
RewriteEngine on
Redirect ^url/url/(.*)$ url/url.php?redirect=$1
The basic redirect systax,
redirect accessed-file URL-to-go-to
I have a URL that is like the following:
http://www.example.com/client/project/subdirectory/value/
I would like like a simple way to be able to change/redirect the URL to the following:
http://www.example.com/client/project/#/subdirectory/value/
Once the redirect is complete, the hash needs to be accessible via JavaScript. I'm okay with a full refresh/redirect, just ideally that I write this once and don't have to change it again.
In other words, when the site goes live, the URLs will be structured differently, so that:
http://www.example.com/subdirectory/value/
Will change to:
http://www.example.com/#/subdirectory/value/
Edit:
I have tried using this:
RewriteRule ^profile/?$ #/profile/ [ NC,L]
Which doesn't seem to do anything
Also tried this:
RewriteRule ^profile/?$ /#/profile/ [NC,L]
Which takes me to the root directory
Also tried this:
RewriteRule ^profile/?$ #/profile/ [R,NC,L]
Which adds the whole root path to the server, followed by /%23/profile/
If you have a URL like the following:
http://localhost/tests/redir/subdirectory/value/
And you want to get it redirected to:
http://localhost/tests/redir/#/subdirectory/value/
Place a .htaccess file into the directory of tests/redir with the following:
RewriteEngine On
RewriteBase /tests/redir
RewriteRule ^(.*/)$ #/$1 [R,L,NE]
And you will get the wanted redirect. The R flag plays together with the RewriteBase directive. Also the NE flag is necessary so that you can put # literally into the redirect URI.
The hash is already used by page anchor tags. You might need to replace it with an entity or pick a better character.