Hey guys i'm trying to convert url which looks like this
example.com/sometype/author-name/sort-date
or
example.com/sometype/author-name/sort-date/something-value/
Just need a generic solution.
I'm pretty new to this and a little weak at regular expressions.
So what i could write was this to identify
sometype/author-name/sort-date/
or
sometype/author-name/sort-date/something-value/
/([a-zA-Z0-9]+)/ ([a-zA-Z0-9]+)-([a-zA-Z0-9]+)
I want to rewrite this into this format
example.come?data={"par1":"sometype","author":"name","sort":"date","something":"vale"}
or
example.come?data={"par1":"sometype","author":"name","sort":"date"}
First understand what you're asking is very unusual and extremely tricky for mod_rewrite. Not only it required recursion based code but also this code won't be very readable.
With that cautionary note enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)(.*?)/?$ $1\",\"$2$3 [L]
RewriteRule ^([^-]+)-([^,]+)(.*)$ $1\":\"$2$3 [L]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ /?data={\"part1\":\"$1\"} [L]
Related
I'm new on .htaccess and rewrite rules.
So if my question is not relevant, please forgive me.
I have below htaccess code.
RewriteRule ^([^/]+)/([^/]+)/?$ article-list.php?link=$1&page=$2 [L,QSA]
If i visit url like www.example.com/category/0 it works.
But if i strip page url and last slash www.exapmle.com/category i see an ugly 404 page.
What is wrong with my htaccess directive?
Thanks in advance.
EDIT: Compeletely .htaccess
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]
RewriteRule ^haber/([^/]+)-([^/]+)/?$ article.php?link=$1&i=$2 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ article-list.php?link=$1&page=$2 [L,QSA]
This is a correct behaviour.
Actually, in a regular expression, a + means at least one.
When you use ([^/]+) it means at least one character which is not a slash.
Your rule ^([^/]+)/([^/]+)/?$ means at least one character which is not a slash / at least one character which is not a slash optional slash.
That's why it does not work with only the first part url.
If you also want to handle example.com/category you'll need another rule:
RewriteRule ^([^/]+)/?$ article-list.php?link=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ article-list.php?link=$1&page=$2 [L]
I am trying to rewrite page URL's with .htaccess.
I would like to transform links like this (mysite.com/page.php -> mysite.com/page)
This is the code which I've used in previous sites with the same requirements
RewriteEngine on
RewriteCond /%{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9_-\s]+)/$ /$1.php
But when I try to access mysite.com/page it leeds to a 404 page where as mysite.com/page.php will show the page.
Can anyone explain if I have missed a setting somewhere? I have placed the .htaccess file in the site root dir and the permissions are set as 644.
Instead of using %{REQUEST_FILENAME} which by the way you have an unneeded / there, I suggest you to use %{DOCUMENT_ROOT}/$1\.php, here is an example:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
## To internally redirect /anything to /anything.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^([^/]+)$ $1.php [L]
Additionally on your rule you have /$ which would again cause you trouble as you want to catch /page.
You could have made it /? to make the / optional.
Your .htaccess fixed should look like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([a-z0-9_-\s]+)/?$ /$1.php [NC,L]
I am trying to strip down
http://host.name/html/about.html
to
http://host.name/about.html
But I keep encountering a 404 error with the following in my .htaccess file:
Options +FollowSymLinks
RewriteEngine on
rewritebase /
RewriteRule ^html/(.*) $1 [R,L]
http://htaccess.madewithlove.be/ says its valid, and i think its valid, but there is clearly something I missed.
Can anyone correct me?
Edit:
It also bounces to root when I try to visit /html/
You have it backwards. Since the actual resource is at /html/about.html, you need to internally rewrite to that URI, not from. Once the internal rewrite is in place, you can externally redirect any direct requests to the html directory. So something like:
Options +FollowSymLinks -Multiviews
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /html
RewriteRule ^html/(.*)$ /$1 [L,R=301]
RewriteCond %{DOCUMENT_ROOT}/html%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/html%{REQUEST_URI} -d [OR]
RewriteCond %{DOCUMENT_ROOT}/html%{REQUEST_URI} -s
RewriteRule ^(.*)$ /html/$1 [L]
Or you could replace the last set of conditions/rule to:
RewriteCond %{REQUEST_URI} !^/html/
RewriteRule ^(.*)$ /html/$1 [L]
I have several urls on a Joomla site which have been indexed and I need to 301 redirect them into some new pages. The old URL is formed like this:
http://www.mydomain.com/en/wfmenuconfig/family/family-disease/177-category-english?start=20
I want it to go to:
http://www.mydomain.com/en/family-members/family-disease
I tried using:
RewriteCond %{QUERY_STRING} ^start=(.*)$
RewriteRule ^/en/wfmenuconfig/family/family-disease/177-category-english$ http://www.www.mydoamin.com/en/family-members/family-disease%1 [R=301,L]
I've tried several answers on here but nothing seems to be working.
htaccess 301 redirect dynamic url
and
301 Redirecting URLs based on GET variables in .htaccess
Any ideas what I should try next? (I've tried a normal redirect 301)
You've almost got it. You need to remove the leading slash from your rule's pattern because it's removed from the URI when applying rules from an htaccess file:
RewriteCond %{QUERY_STRING} ^start=(.*)$
RewriteRule ^en/wfmenuconfig/family/family-disease/177-category-english$ /en/family-members/family-disease%1? [R=301,L]
You also don't need the http://www.www.mydoamin.com bit (2 sets of www). At the end of your target, you have family-disease%1, which means if start=20 then the end of your URL will look like: family-disease20. Is that right?
The new URL doesn't have the query string in it, so it is just stripping of the last URL path part. If you want it hardcoded
RewriteCond %{QUERY_STRING} ^start=
RewriteRule ^en/wfmenuconfig/family/family-disease/177-category-english$ /en/family-members/family-disease? [R,L]
or a little bit more flexible
RewriteCond %{QUERY_STRING} ^start=
RewriteRule ^en/wfmenuconfig/family/family-disease/.+$ /en/family-members/family-disease? [R,L]
or if you just want to keep two levels after en/wfmenuconfig
RewriteCond %{QUERY_STRING} ^start=
RewriteRule ^en/wfmenuconfig/(.+?/.+?)/ /en/$1? [R,L]
Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.
If you just want to redirect http://www.mydomain.com/en/wfmenuconfig/family/family-disease/177-category-english?start=$var into http://www.mydomain.com/en/family-members/family-disease, then you must try these directives:
# once per .htaccess file
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{QUERY_STRING} start=([0-9]*)
RewriteRule ^en/wfmenuconfig/family/family-disease/177-category-english /en/family-members/family-disease [R=301,L]
But if that's not what you want, but to redirect http://www.mydomain.com/en/wfmenuconfig/family/family-disease/177-category-english?start=$var into http://www.mydomain.com/en/family-members/family-disease$var then you could check this one:
# once per .htaccess file
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{QUERY_STRING} start=([0-9]*)
RewriteRule ^en/wfmenuconfig/family/family-disease/177-category-english /en/family-members/family-disease%1 [R=301,L]
Now, give this one a little more try if it will work. If it's not, then find any suspicious why this code is not working:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /en/
RewriteCond %{QUERY_STRING} start=([0-9]*)
RewriteRule ^wfmenuconfig/family/family-disease/177-category-english /family-members/family-disease [R]
And go to http://www.mydomain.com/en/wfmenuconfig/family/family-disease/177-category-english?start=$AnyNumber if it's redirecting into http://www.mydomain.com/en/family-members/family-disease just make sure that your web server have mod_rewrite.
I just wanted to throw this out there, I was also having trouble getting the RewriteRule to work. I have a client that upgraded to a WordPress powered site from .asp pages. What I had to do to get this to work is insert the RewriteCond and RewriteRule in the htaccess file BEFORE the "# BEGIN WordPress" section. Now it works just as it should.
This is posted way late, but hopefully it helps someone else out there running into the same issue.
Doesn't Work:
# BEGIN WordPress
<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
RewriteCond %{QUERY_STRING} ^var=somestring$ [NC]
RewriteRule ^oldpage\.asp$ http://www.domain.com/newpage? [R=301,L]
Does Work:
RewriteCond %{QUERY_STRING} ^var=somestring$ [NC]
RewriteRule ^oldpage\.asp$ http://www.domain.com/newpage? [R=301,L]
# BEGIN WordPress
<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
Order of operations must be important =)
I have a working rewrite rule to hide index.php?dir= from the URL.So for instance if I try
www.example.com/folder/dir1/
it rewrites it to
www.example.com/folder/index.php?dir=dir1/
and that fine!The trouble is if I remove the trailing slash from the URL i.e.
www.example.com/folder/dir1
it goes into a redirection loop!My complete htaccess is:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /folder
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{QUERY_STRING} ^dir=(.*)$ [NC]
RewriteRule ^ %1? [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)/? index.php?dir=$1 [L,QSA]
Please advice?
(i) I am confused about the RewriteBase /papers. This only makes sense in DOCROOT/papers/.htaccess. If this the location and is "folder" == papers? If not then, I am not surprised that the rewrite engine is getting confused. (ii) `%{REDIRECT_STATUS} is not 200 on a subquery lookup to evaluate the default if MultiViews or DirectoryIndex is a match.
So before you do anything else:
Validate that your base is correct, and if not fix it.
Use Options -MultiViews if you don't use them.
Check your system, vhost config and DOCROOT/.htaccess to see if a DirectoryIndex is specified. (Unlike rewrite rules which are only taken from the lowest .htaccess, all are scanned for directives such as this.)
Replace the RewriteCond %{ENV:REDIRECT_STATUS} 200 by
RewriteCond %{ENV:REDIRECT_END}%{IS_SUBREQ} true
and add the flag E=END:true to any rules that you want to force to end of the cycle as a match (similar to the Apache 2.4 [END] flag) The extra %{IS_SUBREQ} prevents the rules being fired on a subquery. You don't want this to happen unless you really know what you are doing.
Figured it out!
Had to replace
RewriteRule ^(.+)/? index.php?dir=$1 [L,QSA]
with
RewriteRule ^(.*)? index.php?dir=$1/ [L,QSA]
Thanks everyone for contributing..