Could something like this be done? No matter what page or directory page=$1 appears under it'll be rewritten/redirected to /$1
For example:
file.php/1 would be file.php?page=1
dir/file/2 would be dir/file?page=2
dir/file.php?name=something/3 would be dir/file.php?name=something&page=3
Here's what I have so far:
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteCond %{QUERY_STRING} ^&page=([0-9-]+)/?$
RewriteRule ^(.+)\.php$ $1/%2 [R=301,L]
RewriteRule ^(.*)$ $1.php/$2
To cover the URI paths that end with /123 we can use this rule:
RewriteRule ^(.+)/([0-9]+)$ /$1?page=$2 [QSA,L,R]
So:
file.php/1 would be file.php?page=1
dir/file/2 would be dir/file?page=2
Note that this will also cover: dir/file/3?foo=bar would be dir/file?page=3&foo=bar
To cover the /123 that gets appended at the end of the actual query string, we can use this rule:
RewriteCond %{QUERY_STRING} (.*)/([0-9]+)$
RewriteRule ^(.+)$ /$1?%1&page=%2 [L]
So:
dir/file.php?name=something/3 would be dir/file.php?name=something&page=3
Related
i have a issue with some rewriting url with htaccess.
i need the following:
old: mydomain.com/categorie/details.php?companyid=10
new: mydomain.com/item/?p=10
where i want to catch and replace the numbers (of several id) dynamically.
I've read the answer of the other question but that's only replace the query string. I quess i need some regex but i'm a newbee with that..
i have this in htaccess:
RewriteCond %{REQUEST_URI} ^/categorie/details\.php$
RewriteCond %{QUERY_STRING} ^companyID=([0-9]*)$
RewriteRule ^(.*)$ www.mydomain.com/item/?p=%1/ [R=301,L]
Before i migrate i've tested this and it worked fine, for some reasons it won't work anymore after migration.
Do i miss something? Is this the wrong approach?
Kind regards,
Tom.
At this line
RewriteRule ^(.*)$ www.mydomain.com/item/?p=%1/ [R=301,L]
Put the ful URL like this :
RewriteRule ^(.*)$ http://www.example.com/item/?p=%1/ [R=301,L]
Also put this [NC] ,nocase to accept upper-case and lower-case, at the end of this line
RewriteCond %{QUERY_STRING} ^companyID=([0-9]*)$ [NC].
So , your rules should look like this :
RewriteCond %{REQUEST_URI} ^/categorie/details\.php$
RewriteCond %{QUERY_STRING} ^companyID=([0-9]*)$ [NC]
RewriteRule ^(.*)$ http://www.example.com/item/?p=%1 [R=301,L]
To sumerize all these rules more , do this :
RewriteCond %{QUERY_STRING} ^companyID=([0-9]*)$ [NC]
RewriteRule ^categorie/details\.php$ http://www.example.com/item/?p=%1 [R=301,L]
And as you menthioned that it is same domain , let them be like this :
RewriteCond %{QUERY_STRING} ^companyID=([0-9]*)$ [NC]
RewriteRule ^categorie/details\.php$ /item/?p=%1 [R=301,L]
No need to full URL.
Note: clear browser cache then test
How do i remove a query string from the end of a url?
To be more specific, this is my rewrite rule:
RewriteRule example-(.*).html$ examples/view-example.php?param1=parameter¶m2=$1&split=-
and I want this to return a 404 or a redirect to www.mydomain.com/example-one.html :
www.mydomain.com/example-one.html?param1=parameter¶m2=one&split=-
This is what i tried, it doesn't work:
RewriteCond %{REQUEST_URI} /examples/view-example\.php
RewriteCond %{QUERY_STRING} param1=parameter¶m2=(.*)&split=-
RewriteRule ^(.*)$ http://mydomain.com/example-%1.html$
I think that RewriteRule ^(.*)$ http://mydomain.com/example-%1.html$ isn't correct..
This should do the trick:
RewriteCond %{QUERY_STRING} ^param1=parameter¶m2=(.*)&split=-
RewriteRule ^/examples/view-example\.php$ http://mydomain.com/example-%1.html [R=301]
Though, I didn't understand what you wanted to do with www.mydomain.com/example-one.html?param1=parameter¶m2=one&split=-
I got the following url:
127.0.0.1/abc_123456/default/index/index/
Which should be rewritten to:
127.0.0.1/123456/index.php/default/index/index/
So remove abc_ and add index.php after it. Problem is that the digits are variable, but the abc_ isn't.
I had the following rule:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /abc_
RewriteRule ^abc_(.*)/(.*)$ /$1/index.php/$2
But that resulted in the url being rewritten to:
127.0.0.1/123456/default/index/index.php/index/
Seems like I'm almost there, but I can't figure it out.
Thanks in advance
Use this simple rule:
RewriteRule ^abc_([0-9]+)/(.*)$ $1/index.php/$2 [L,NC]
Try
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /abc_([0-9]+)/([^\ \?]+) [NC]
RewriteRule ^ /%1/index.php/%2 [L]
EDIT
However, you are right about the other rule, that's the one giving the error; RewriteCond %{THE_REQUEST} !^[A-Z]+\ /abc_ RewriteRule ^(.*)$ /index.php/$1 That one is used to rewrite if the page does not contain the /abc_123456/
Add an extra condition to that rule as below
#if not abc_
RewriteCond %{THE_REQUEST} !^[A-Z]+\ /abc_ [NC]
#if not already index.php
RewriteCond %{REQUEST_URI} !^/index\.php$ [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
for example, if you need similar solution, when visited this url:
http://yoursite.com/subpage1/subpage2/?YOURSTRING=blabla
to redirected visitor to
http://yoursite.com/subpage1/subpage2/
then see link - http://stackoverflow.com/a/15680832/2215124
I want users who type
http://www.example.com/word-of-the-day
to be taken to
http://www.example.com/index.php?page=word-of-the-day
But I want
http://www.example.com/word-of-the-day
to be shown in the URL for the user.
What should I do in my .htaccess? I tried but the regular expression
and the syntax of RewriteRule is way too complicated for me to
figure out how to do it.
Any help will be appreciated.
Edit:
Also, how can I say this in htaccess -
if they type http://www.example.com/word-of-the-day, take them to http://www.example.com/index.php?page=word-of-the-day
or if they type http://www.example.com/something-else, take them to http://www.example.com/index.php?page=something-else
or else, just take them to the URL they typed.
The condition below checks that index.php is not being requested. If not apply the rule. This will work for any of the scenarios you listed above.
RewriteCond %{QUERY_STRING} ^!.*[index\.php].*$ [NC]
RewriteRule ^(.*)$ index.php?page=$1 [L]
In response to your comment about only wanting to do this for a few specific pages, it would look like this(as an alternative to Nils edit):
RewriteCond %{QUERY_STRING} ^!.*[index\.php].*$ [NC]
RewriteCond %{REQUEST_URI} ^word-of-the-day$ [OR]
RewriteCond %{REQUEST_URI} ^something-else$ [OR]
RewriteCond %{REQUEST_URI} ^even-something-else$
RewriteRule ^(.*)$ index.php?page=$1 [L]
Try this
RewriteEngine on
RewriteRule ^word-of-the-day$ index.php?page=word-of-the-day
Or more flexible
RewriteEngine on
RewriteRule ^(.*)$ index.php?page=$1
Not tested, yet it sould work.
To your edit:
Just define those specific URLs manually:
RewriteEngine on
RewriteRule ^word-of-the-day$ index.php?page=word-of-the-day
RewriteRule ^word-some-example$ index.php?page=some-example
RewriteRule ^some-other$ index.php?page=some-other
How could I use a rewrite to change:
/?tag=foo
To:
/tag/foo
I tried:
RewriteCond %{QUERY_STRING} ^tag=(.+)$
RewriteRule ^(.*)$ http://www.example.com/tag/$1 [L]
But it did not work.
Try the following:
RewriteCond %{QUERY_STRING} ^tag=(.+)$
RewriteRule ^(.*)$ http://www.example.com/tag/%1 [L]
Usually, rewrites are used to achieve the opposite effect. Are you sure you don't really want to do the following?
RewriteRule ^tag/(.+)$ index.php?tag=$1 [L]
To avoid recursion, you should check the request line instead as the query string in %{QUERY_STRING} may already have been changed by another rule:
RewriteCond %{THE_REQUEST} ^GET\ /\?(([^&\s]*&)*)tag=([^&\s]+)&?([^\s]*)
RewriteRule ^(.*)$ /tag/%3?%1%4 [L,R=301]
Then you can rewrite that requests back internally without conflicts:
RewriteRule ^tag/(.*) index.php?tag=$1 [L]
I was trying to convert from a URL like this:
http://java.scandilabs.com/faq?key=Contents_of__gitigno
To a URL like this:
http://scandilabs.com/technology/knowledge/Contents_of__gitigno
Andrew's response above worked for me with the addition of a question mark at the end to discard the original query string:
RewriteCond %{HTTP_HOST} ^java
RewriteCond %{QUERY_STRING} ^key=(.+)$
RewriteRule ^(.*)$ http://scandilabs.com/technology/knowledge/%1? [R=301,L]
Note if you're on apache 2.4 or higher you can probably use the QSD flag instead of the question mark.