I have searched for hours for a tutorial to learn how I can rewrite an image path url containing a "?".
My images look like this:
<img src="/index.php?rex_resize=600w__/imageName.jpg" >
For the sake of caching all images, I must/want to remove this "?" from the string.
This is the rule I used:
RewriteRule ^rex_resize/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)\.(gif|jpg|jpeg|png)$ /index.php?$1/$2.$3 [NC]
This is all the content from my .htaccess with my rewriteRule inside:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^rex_resize/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)\.(gif|jpg|jpeg|png)$ /index.php?$1/$2.$3 [NC]
RewriteRule ^sitemap\.xml$ index.php?rexseo_func=googlesitemap [NC,L]
RewriteRule ^robots\.txt$ index.php?rexseo_func=robots [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !^redaxo/.*
RewriteCond %{REQUEST_URI} !^files/.*
RewriteCond %{REQUEST_URI} !^google(.*).html*
RewriteRule ^((.|\r|\n)+)/? index.php?params=$1 [L,NC]
</IfModule>
But since I've never learned proper RewriteRules, I get no match from this RewriteRule, and my knowledge about this is very limited.
For some help I'm thankful :)
You need to figure out a pattern to match against in your URL, to know you have to rewrite current request to your image script.
According to your post and commnents, let's say that the pattern is :
All URIs...
begining with files/
ending with a web image suffix (gif, jpg, png)
with something between those
Write this down into a regexp and your done :
/ : Begining of URI
files/ : our fake directory
(.*) : captures anything (will be stored in $1)
\. : a dot
(gif|jpg|jpeg|png) : file suffix (will be stored in $2)
$ : End of URI
RewriteRule :
RewriteRule ^/files\/(.*)\.(gif|jpg|jpeg|png)$ index.php?rex_resize=$1.$2 [NC,L]
Examples :
Incoming request:
/files/600w/imageName.jpg
Request after Mod_Rewrite:
/index.php?rex_resize=600w/imageName.jpg
Note that your example shows a redirection to /index.php (begining with a slash) which should not work when you use a .htaccess file (it only does when having rules in whost config)
Related
I would like to redirect all URL having a trailing slash referenced by google to the new url without a trailing slash.
Example :
http://example.com/toto/ ===> to http://example.com/toto
Warning I already rewrite the rule to avoid the .html extension also !
Here is my existing code :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteCond %{REQUEST_URI} (.+)$
RewriteRule ^ %1 [R=301,L]
</IfModule>
I got the following error :
Not Found
The requested URL /example/.html was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
The objectif of the topic was to find a solution to make a redirection of existing URL listed by search engine like google to not lose the current SEO after making a complete refresh of the web site (Wordpress to bootstrap).
The first objective was to redirect all url trafics like http://example.com/toto/ (google) ===> to http://example.com/toto.
Unfortunatly I understood that making the reverse is more powerful to not lose my existing listing on google (sitemap & robots)
So I have decided to review all the code in order to have the following result by deleting the html extension and adding a trailing slash at the end of each url :
http://example.com/toto.html ===> to http://example.com/toto ==> http://example.com/toto/
Below is the code HTaccess that I used to make this possible :
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.html to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1/ [R,L]
# add a trailing slash at the end /dir/foo/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]
# delete the extension .html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*?)/?$ $1.html [L,QSA]
I have also added the code line below in each html page to enforce it :
<base href="http://example.com/">
Hoping to help any readers/coders in the same situation
Recently I redesigned my site let's say http://www.sitename.com/ .
Before the redesign, the homepage url was something like this: http://www.sitename.com/default.asp?id=1&lg=1
Old pages had also weird query strings and they are not relevant any more, so, I want to redirect everything that begins with default.asp to the homepage.
RewriteRule default\.asp.* \
alternatively
RewriteCond %{QUERY_STRING} ^default.asp?id=1&lg=1$ [NC]
RewriteRule http://www.sitename.com/ http://www.sitename.com/? [R=301,L]
This is the closest I have got so far, but I am pretty sure its wrong.
Can you help?
Update: This is my whole .htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule default\.asp.* /?
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I have tried putting my rule at the top and at the bottom, (no luck). Should I embed it somehow on the other rule?
You can use these rules:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# redirect /default.asp to landing page
RewriteRule default\.asp$ /? [L,NC,R=301]
RewriteRule ^index\.php$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
# END WordPress
Based on this site, if you set up the rule like this
RewriteRule default\.asp.* /?
it should work.
Here is the reference for how you can replace query strings in the rewrite:
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule
Modifying the Query String
By default, the query string is passed through unchanged. You can,
however, create URLs in the substitution string containing a query
string part. Simply use a question mark inside the substitution string
to indicate that the following text should be re-injected into the
query string. When you want to erase an existing query string, end the
substitution string with just a question mark. To combine new and old
query strings, use the [QSA] flag.
UPDATE
Based on your comment, try this:
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule default\.asp.*$ /? [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
Note the [L] at the end of RewriteRule default\.asp.*$ /? [L] which terminates the rewriting process when that match is found. See L flag
If you don't include the L flag, then the process will continue with the rest of the rules in your .htaccess until it reaches the end with no matches or until it matches one with the L flag. Think of it like a switch statement, which needs a break in each case or else it continues to the next case, if it helps you.
default.asp is a filename you need to match against it using %{REQUEST_URI} variable. it's not part of querystring. To redirect requested URIs that contain default.asp ,you can use the following :
RewriteCond %{REQUEST_URI} /default\.asp [NC]
RewriteRule ^ http://sitename.com/? [R,L]
I am having a problem with my site. Sometimes after the user log in is redirected to a page like this:
mydomain.com//somepage
Please notice the double slash in the URL, which takes to a not valid page so i want to modify my htaccess in order to make all pages with double slash to automatically redirect to an URL like this:
mydomain.com/folder/somepage
Please notice the word "folder" between slashes this time. An URL like this would always take to a valid page.
I made some rewrite rules for my htaccess but they dont work as expected:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{THE_REQUEST} \ //([^\?\ ]*)
RewriteRule ^ /folder/%1 [L,R=301]
</IfModule>
Could you please give me a hint?
Thank you.
Try something like this:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ //([^\?\ ]*)
RewriteRule ^ /folder/%1 [L,R=301]
You need to match against the %{THE_REQUEST} variable because the URI gets normalized before it gets matched against the pattern of rewrite rules.
i am trying to create an .htaccess file that will achieve the following
create a clean url like this www.mydomain.com/About-Us with page page.php?title=About Us
query a database with the parameters passed on the url like this www.mydomain.com/?search=abc and it pulls to page index.php?search=abc
this is my code so far
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^?search=([0-9]*)$ index.php?id=$1 ## e.g www.mydomain.com/?search=try
RewriteRule ^([A-Z]*)$ page.php?name=$1
## www.mydomain.com/About-Us
##ErrorDocument 404 PageNotavailabale
####Protect the system from machines with worms
RewriteRule (cmd|root)\.exe - [F,E=dontlog:1]
#### To hold and redirect css/images/js files
RewriteRule images/(.+)?$ images/$1 [NC,L]
RewriteRule css/(.+)?$ css/$1 [NC,L]
RewriteRule js/(.+)?$ js/$1 [NC,L]
Why not using this kind of url for your search engine : www.domain.com/search/abc ?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^search/([a-zA-Z0-9]+)$ index.php?search=$1
RewriteRule ^([a-zA-Z0-9]+)$ index.php?page=$1
Then, your access your pages with www.domain.com/<myPage>.
And your search engine with www.domain.com/search/<mySearch>
EDIT :
Please notice your rules doesn't allow a lot of params :
^?search=([0-9]*)$ allows only numbers (even an empty parameter)
^([A-Z]*)$ allows only uppercase letters (and also empty parameter)
What I'm trying to achive is to have all urls on my page look like http://domain.com/page/, no extensions, but a trailing slash. If a user happends to write http://domain.com/page or http://domain.com/page.php it will redirect to the first url. After some googling i found this code, and it's close to working, but when you leave out the trailing slash in your request the url becomes something like http://domain.com/Users/"..."/page/ and therefor returns a 404.
My .htaccess looks like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !(.*)/$
RewriteRule (.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
I've been trying to add an additional rule but I really don't get any of this and I haven't been able to find any answers.
For a scenario like this one, the .htaccess author has to consider both what the browser URL bar should display and what file the web server should return/execute. Note also that each external redirect starts the processing of the rewrite directives over.
With that in mind, start by taking care of which file is returned when the URL is in the correct format:
RewriteEngine on
RewriteRule ^/?$ /index.php [L]
RewriteRule ([^./]+)/$ /$1.php [L]
Then, deal with URLs with no trailing slash by redirecting them with [R=301]:
RewriteRule ^/(.*)\.[^.]*$ http://www.example.com/$1/ [R=301,L]
RewriteRule ^/(.*)$ http://www.example.com/$1/ [R=301,L]
Note that the first of these two rules should also take care of the case where there is a filename (like something.php) but also a trailing slash by eliminating the filename extension and re-adding the slash.
Keep in mind that, if your internal directory structure does not match what the web server is serving (as is often the case in shared hosting scenarios), you will likely need to add a RewriteBase directive immediately after the RewriteEngine directive. See the Apache docs for an explanation.