I was able to get the urls to work using this code
RewriteRule ^(.*)/([^/\.]+)/?$ $1/index.shtml?dynContent=$2 [L]
So dynContent = apples or bananas or bananas-apples
So if I type in http://example.com/dir1/apples or /dir1/bananas or /dir1/bananas-apples it displays using the correct content I am pulling into index.shtml
However I have one page that needs to be full page with and I have one page configured to go full page and it is named as indexFP.shtml
The dynContent=oranges for this one page
I have the RewriteRule as
RewriteRule ^(.*)/([^/\.]+)/?$ $1/indexFP.shtml?dynContent=$2 [L]
But this does not call up the correct page with full page for this one page.....
When I type in http://example.com/dir1/oranges it uses index.shtml instead of indexFP.shtml
Is there a way to do this? Here is what I have now.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/([^/\.]+)/?$ $1/index.shtml?dynContent=$2 [L]
RewriteRule ^(.*)/([^/\.]+)/?$ $1/indexFP.shtml?dynContent=$2 [L]
Any suggestions?
[L] means "last" in mod_rewrite. That stops processing so your indexFP rule above will never get hit.
What I think you are trying to do is choose index for some strings and indexFP for others. To do that you need a way to choose one rule over the other. My rewrite-fu is a little rusty but something like:
# match "oranges"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /oranges/
RewriteRule ^(.*)/([^/\.]+)/?$ $1/indexFP.shtml?dynContent=$2 [L]
# match everything else
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/([^/\.]+)/?$ $1/index.shtml?dynContent=$2 [L]
Related
Friends, what is the right way to get both the php files to open friendly URL contents?
My current code works OK if I only use:
Options +FollowSymLinks
RewriteEngine On
# SEO URL Settings
RewriteBase /site/
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ post.php?id=$1 [QSA,L]
but then I also need friendly URL for my categories so I tried to add:
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ cat.php?cat=$1 [QSA,L]
but doing so only the post URL opens but category links redirect back to index.php but if you remove the rewrite for post than the cat.php contents shows.
If someone could help me out here would really appreciate your kindness.
This is because of your use of the [L] flag.
The [L] flag causes mod_rewrite to stop processing the rule set. In
most contexts, this means that if the rule matches, no further rules
will be processed. This corresponds to the last command in Perl, or
the break command in C. Use this flag to indicate that the current
rule should be applied immediately without considering further rules.
Documentation
Instead, try and have your rules laid out like this:
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ post.php?id=$1 [QSA]
RewriteRule ^(.*)$ cat.php?cat=$1 [QSA,L]
I have a .htaccess file that creates a SEO URL. For example, let's say the ugly URL is: example.com/stories?url=hello-world&page=5 the URL becomes example.com/stories/hello-world/5
This works perfectly, but for the first page I want the URL to not display the page number. For example, I want a URL like this example.com/stories/hello-world/1 to be example.com/stories/hello-world How do I do this?
Current .htaccess
RewriteEngine On
RewriteBase /stories/
RewriteCond %{THE_REQUEST} /\?url=([^&\s]+)&page=([0-9]+) [NC]
RewriteRule ^ %1/%2? [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?url=$1&page=$2 [L,QSA]
Try this it like this,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# for hello world first page we are giving static value 1 to page.
RewriteRule ^([\w-]+)/?$ index.php?url=$1&page=1 [QSA,L]
# for others we are getting dynamic value.
RewriteRule ^([\w-]+)/([\d]+)$ index.php?url=$1&page=$2 [QSA,L]
I'm trying to allow my site to rewrite urls. I have put the following into my .htaccess file in the root directory.
RewriteEngine On
#would be nice to remove member-pages from the URL but no idea how.
#RewriteRule ^members/(.*)/?$ /$1 [NC,R]
#This part works though!
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/?$ ./members/$1/ [L]
So far, it takes
mydomain.com/someUserName or mydomain.com/someUserName/ (with trailing slash) and, if it exists, will load the page at mydomain.com/members/someUserName/ without a hitch. This works like a gem.
What I want now (and am trying to do with the first rewrite rule) is to take a mydomain.com/members/someUserName or mydomain.com/members/someUserName/ and have it show up as mydomain.com/someUserName in the url.
How do I do this? Thanks in advance!
If I understand you correctly, You want to redirect domain.com/members/foo to domain.com/foo , You can use the following rule for that:
RewriteEngine On
RewriteCond %{THE_REQUEST} /memebers/([^\s]+) [NC]
RewriteRule ^ /%1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ ./members/$1 [NC,L]
I am trying to write a rewrite condition where
It must NOT redirect if I see Server in the URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !^/Server/(.*)$ /redirect.php?page=$1 [L]
But I also want it to go to redirect.php when I dont have Server there. However, the $1 variable is never picking out the content that I am entering. it is always blank
Why?
(.*) is never filled, or, better, it's only filled in the case you do not want it, as it is only filled when you have Server in the url.
So I think the thing you want is a rewriteCond excluding urls containing Server and after that a rewriteRule catching all the content and using it as an argument, this way:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/Server/ [NC]
RewriteRule (.*) /redirect.php?page=$1 [L]
What I'm trying to achieve:
1) http://localhost/en/script.php?param1=random is mapped to http://localhost/script.php?param1=random&language=English
This has to work always.
2) http://localhost/en/random/text/here will be mapped to http://localhost/categories.php?term=random/text/here
This has to work if random/text/here is 404
What I have at the moment:
RewriteEngine on
RewriteCond substr(%{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^en/(.+)$ categories.php?lang=English&terms=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ee/(.+)$ categories.php?lang=Estonian&terms=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^fi/(.+)$ categories.php?lang=Finnish&terms=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ru/(.+)$ categories.php?lang=Russian&terms=$1 [L]
RewriteRule ^en/(.*) $1?lang=English [QSA]
RewriteRule ^ee/(.*) $1?lang=Estonian [QSA]
RewriteRule ^ru/(.*) $1?lang=Russian [QSA]
RewriteRule ^fi/(.*) $1?lang=Finnish [QSA]
What is the problem:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
It's supposed to redirect to categories.php?lang=English IF /en/this/here/does/not/match/a/script. If I load an URL like en/index.php it will also get mapped to categories.php?lang=English because en/index.php does not exist.
What I've thought:
substr(%{REQUEST_FILENAME},3) would fix my problem (as currently /ee/index.php is literally mapped to /ee/index.php instead of just /index.php)
Unfortunately I couldn't find a way to manipulate strings :/
I take it the language code is what makes the URL map to a non-existant file. Switch the two steps, moving the language code to the query string first. This also has the added advantage of simplifying the keyword step to a single RewriteRule, since they no longer need to do two things at once.
RewriteRule ^en/(.*) $1?lang=English [QSA,DPI]
RewriteRule ^ee/(.*) $1?lang=Estonian [QSA,DPI]
RewriteRule ^ru/(.*) $1?lang=Russian [QSA,DPI]
RewriteRule ^fi/(.*) $1?lang=Finnish [QSA,DPI]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ categories.php?terms=$1 [L,QSA]
the issue is that you are using the L flag. Which means that rule will be the last to be executed.
also
%{QUERY_STRING}
isnt necessary, add QSA and you will get all parameters added to the end of the url
try to do:
RewriteRule ^en/(.*) $1?lang=English [QSA]
For substr problem, you could try absolute paths:
RewriteRule ^en/(.*) /$1?lang=English&%{QUERY_STRING}
or
RewriteRule ^en/(.*) http:/localhost/$1?lang=English&%{QUERY_STRING}
Also, I might be nitpicking, but wouldn't it be easier if you did the language evaluation in the PHP base on language codes, 404'ed non-existent languages and used
RewriteRule ^(.*?)/(.*) $2?lang=$1&%{QUERY_STRING}
Edit: depending on how many scripts you have, can't you do something like:
RewriteRule ^(.*?)/(script.php|other.php) $2?lang=$1 [QSA]
= have pipe-list files, that are accesible?