i have a rule like this:
RewriteRule ^posts/(.*)/([0-9]*)$ viewupdates.php?username=$1&page=$2 [L]
and match to: http://site.com/posts/username/1
i need to chante to:
http://site.com/username/posts
without trailing slash, and if have more pages /posts/1
Simply transpose the corresponding parts of the regexp for the latter case:
RewriteRule ^(.*)/posts/([0-9]*)$ viewupdates.php?username=$1&page=$2 [L]
Probably, you also want another rule without the extra "page" specification (for the former case you describe), smth like this:
RewriteRule ^(.*)/posts$ viewupdates.php?username=$1 [L]
or
RewriteRule ^(.*)/posts$ viewupdates.php?username=$1&page=1 [L]
depending on your implementation of the script.
Related
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ category.php?slug=$1
RewriteRule ^([^/\.]+)/([^/\.]+)$ product-details.php?slug1=$1&slug=$2
RewriteRule ^([^/\.]+)/?$ infrastructure-details.php?slug=$1
what I have already tried
This is my htaccess file. problem is when I am trying to execute (infrastructure-details.php?slug=$1) its move to (category.php?slug=$1) conflict with first rule of htaccess.
I tired multiple rewrite methods but its not working. Please help to solve this issue.
localhost/project/category.php?slug=pump, localhost/project/infrastructure-details.php?slug=paint second url i want to be-> localhost/project/paint both page is different. can you please specify how to write rules for this different pages.
There is no discernible pattern that differentiates these two URLs so the only way to implement these two rewrites is to hardcode them. For example:
RewriteRule ^pump$ category.php?slug=$0 [L]
RewriteRule ^paint$ infrastructure-details.php?slug=$0 [L]
Where the $0 backreference in the substitution string contains the entire match by the RewriteRule pattern (just saves some repetition).
If you need a more general solution (as your directives suggest) then there needs to be a discernible pattern in the URL that differentiates URLs that should be rewritten to category.php and infrastructure-details.php respectively.
I'm assuming your .htaccess file, and other files, are is inside the /project subdirectory.
RewriteRule ^([^/\.]+)/?$ category.php?slug=$1
RewriteRule ^([^/\.]+)/([^/\.]+)$ product-details.php?slug1=$1&slug=$2
RewriteRule ^([^/\.]+)/?$ infrastructure-details.php?slug=$1
Rule #1 and #3 conflict - they use exactly the same pattern (regex) to match against the requested URL. The first rule is always going to "win" and rewrite the request before rule#3 is able to process the request, so rule#3 never matches.
To write a generic rule like this there needs to be a discernible difference between the URL types that you can match with a pattern/regex. For example:
/category/pump
/infrastructure/paint
And then you can construct rules...
Options -MultiViews
RewriteRule ^category/([^/]+)$ category.php?slug=$1 [L]
RewriteRule ^infrastructure/([^/]+)$ infrastructure-details.php?slug=$1 [L]
Note that the order of these directives can be important. More specific rules need to be before more generalised rules.
Options -MultiViews
RewriteEngine on
RewriteRule ^infrastructure/([^/]+)$ infrastructure-details.php?slug=$1 [L]
RewriteRule ^([^/\.]+)/?$ category.php?slug=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)$ product-details.php?slug1=$1&slug=$2 [L]
This is work fine for me. (infrastructure-details.php?slug=$1 [L]) put on top.
I am using Mod Rewrite to create attractive URLs. I am trying to understand how it is possible to ignore parts of a URL?
For example, my original URL was:
/file.php?token=771736eb78bc3b6f96d5c066069567c0c219bf65/
And I have written a Mod Rewrite code to turn that into:
/file/771736eb78bc3b6f96d5c066069567c0c219bf65/
RewriteEngine On
RewriteRule ^file/([^/]*)$ /file.php?token=$1 [L]
However, I would like to achieve a URL like:
/file/771736eb78bc3b6f96d5c066069567c0c219bf65/random-bits-here/
The random bits on the end serve no purpose (other than decoration). How do I tell Mod Rewrite to ignore this part of the URL?
Just tweak your regex like this
RewriteRule ^file/([^/]+)/?(.*)$ /file.php?token=$1 [NC,L]
You should add the second group of characters to your RewriteRule and ignore the caught expression, if you don't need it.
RewriteEngine On
RewriteRule ^file/([^/]*)/([^/]*)/$ /file.php?token=$1 [L]
Or, if you would like to have it passed to your script.:
RewriteEngine On
RewriteRule ^file/([^/]*)/([^/]*)/$ /file.php?token=$1&extra=$2 [L]
Simply leave out the $ sign, which forces the end of the string (path):
RewriteEngine On
RewriteRule ^file/([^/]*) /file.php?token=$1 [L]
quick question for those who know a little more about mod-rewrite than i do:
I have some rules written in sequence like so:
RewriteRule ^(en|fr) index.php?page=home&lang=$1 [L]
RewriteRule ^(en|fr)/home index.php?page=home&lang=$1 [L]
RewriteRule ^(en|fr)/terms index.php?page=terms&lang=$1 [L]
however the first one seems to be over-riding the rest.
I've tried taking off the [L] but this doesnt do what I expected and it will never show the "terms" page.
Any ideas?
If you want http://www.example.com/en or http://www.example.com/fr/ (with or without trailing slash) to redirect to the first item, change the first rule to be:
RewriteRule ^(en|fr)/?$ index.php?page=home&lang=$1 [L]
The /? matches whether or not there's a trailing slash and the $ means "match the end of the URL" (don't match if the URL continues).
I'm trying to rewrite from domain.com/page/soft-15/android-26/ to page.php?cat=15&os=26 with this code:
RewriteRule ^page/([0-9]+)\-([a-zA-Z0-9-]*)/([0-9]+)\-([a-zA-Z0-9-]*)$ page.php?cat=$1&os=$2
I thinks it works fine but, how can i rewrite domain.com/page/ and domain.com/page (without the last forward slash) to domain.com/page.php keeping both rules working?
ok think i got it mixing Jimp & Jon code
RewriteRule ^page/?$ page.php [L]
RewriteRule ^page/(?:[a-zA-Z0-9-]*-)?([0-9]+)/(?:[a-zA-Z0-9-]*-)?([0-9]+)/?$ page.php?cat=$1&os=$2 [L]
RewriteRule ^page/(?:[a-zA-Z0-9-]*-)?([0-9]+)/?$ page.php?cat=$1 [L]
This math with
domain.com/page
domain.com/page/
domain.com/page/soft-15
domain.com/page/soft-15/
domain.com/page/15
domain.com/page/15/
domain.com/page/soft-15/android-26
domain.com/page/soft-15/android-26/
domain.com/page/soft-15/26
domain.com/page/15/26
and so on...
Add a trailing /? to your pattern (before the $ anchor). The ? makes the / optional.
Additionally, your patterns seem backward, matching the digits before the characters. Try this:
RewriteRule ^page/(?:([a-zA-Z\d-]*)-)?(\d+)/(?:([a-zA-Z\d-]*)-)?(\d+)/?$ page.php?cat=$1&os=$2
That should match these variations like these:
domain.com/page/soft-15/android-26
domain.com/page/soft-15/android-26/
domain.com/page/15/android-26
domain.com/page/15/android-26/
domain.com/page/soft-15/26
domain.com/page/soft-15/26/
domain.com/page/15/26
domain.com/page/15/26/
Your backreferences don't seem to jive with the example that you're using:
/page/soft-15/android-26/
to page.php?cat=15&os=26
Your regex looks like it's matching:
/page/15-soft/26-android/
And rewriting to:
page.php?cat=15&os=soft
If you're going by your example, you'd want something like:
RewriteRule ^page/[a-zA-Z0-9-]*?-([0-9]+)/[a-zA-Z0-9-]*?-([0-9]+)/?$ page.php?cat=$1&os=$2
RewriteRule ^page/[a-zA-Z0-9-]*?-([0-9]+)/?$ page.php?cat=$1
RewriteRule ^page/?$ page.php
I have a strange problem with mod_rewrite, the rules that are relevant here are:
RewriteRule ^(.*)\/igre\-(.*)\.php\?Page=([0-9]+)$ game.php?GameUrl=$2&Page=$3 [L]
RewriteRule ^(.*)\/igre\-(.*)\.php$ game.php?GameUrl=$2&Page=1 [L]
And a corresponding URL might look something like this:
example.com/miselne-igre/igre-shirk.php?Page=2
example.com/miselne-igre/igre-shirk.php
The problem is that the first rule has no effect. If I use the first URL from the example I always get 1 into the Page variable, which shows that the second rule is used.
So what's wrong with the first one? And why is the second rule even matching a URL with ".php?Page=XYZ" at the end, if I said that the URL ends with ".php"?
ps: other rules in the .htaccess file are working fine...
The query string is not part of the URI path that is being processed by the RewriteRule directive. You have to use the RewriteCond directive to process the query string.
RewriteCond %{QUERY_STRING} ^Page=[0-9]+$
RewriteRule ^[^/]+/igre-([^/]+)\.php$ game.php?GameUrl=$1&%0 [L]
RewriteRule ^[^/]+/igre-([^/]+)\.php$ game.php?GameUrl=$1&Page=1 [L]
But you can still simplify this by using the QSA flag (query string append):
RewriteRule ^[^/]+/igre-([^/]+)\.php$ game.php?GameUrl=$1 [L,QSA]
mod_rewrite is not using the query in it's rewriting process. Therefor you first RewriteRule is ignored. You could combine it with a RewriteCond (haven't tested it though) like so:
RewriteCond %{QUERY_STRING} Page=([0-9]+)
RewriteRule ^(.*)\/igre\-(.*)\.php\?Page=([0-9]+)$ game.php?GameUrl=$2 [L, qsappend]
# qsappend appends the original query, in this case (Page=xx)
Ah, like Gumbo said; you can also use %1 to back reference to the page numer.
Is it just me or are your arguments back-to-front?
Do you mean:
RewriteRule ^(.*)\/(.*)\-igre\.php\?Page=([0-9]+)$ game.php?GameUrl=$2&Page=$3 [L]
RewriteRule ^(.*)\/(.*)\-igre\.php$ game.php?GameUrl=$2&Page=1 [L]
You wanted to match miselne-igre not igre-miselne.
Obviously this doesn't address the main issue, but thought I'd throw that in.
Dom