I want to get a URL scheme exact like SO's question URL.
Like http://stackoverflow.com/questions/6035363/so-like-url-with-mod-rewrite
this page can be accessed with http://stackoverflow.com/questions/6035363
but then it redirects to the http://stackoverflow.com/questions/6035363/so-like-url-with-mod-rewrite
I need to do the same.
My page will be in the root directory named article.php
the url will be like
article.php?id=1&title=some_text
i want it to be accessed with [any of these will do]
acticle.php/id/1/some_text
or
acticle/id/1/some_text
or
acticle/id/1&title=some_text
[Sorry i really dont know about mod_rewrite so i read some question and googled some cheat sheets but none of them worked :(
So looking for some copy-paste solution]
It's best if you divide and conquer the URL to reverse engineer it.
So a few key points:
6035363 is the important part of the URL as it's the question ID.
Everything else is arbitrary SEO, friendly URLs
Having multiple URLs of the same page will hurt SEO, so stick to one URL format
With that said, article/id/some_keyword_text is the format I suggest. The following RewriteRules will handle it.
RewriteEngine On
RewriteBase /
RewriteRule ^article/(\d+)/?$ test/article.php?id=$1 [L]
RewriteRule ^article/(\d+)/([\w-]+)/?$ test/article.php?id=$1&title=$2 [L]
The first rule redirects article/id with an optional trailing slash. The second rule redirects article/id/some_keyword_text with an optional trailing slash. some_keyword_text may only contain alphanumeric, underscore, or dash characters.
Note: The redirection for URLs of format article/id happens at the page level. So this is not a RewriteRule, but logic in the page that redirects when title is not set. I'm guessing a 301 Redirect to ensure point #3.
Related
I'm on the learning curve for 301 redirects and have done lots of research, including looking at answers on this forum. I haven't found the answer to my specific query, which requires removing elements from the middle of the url request.
Namely, I am building a new site with dynamic links (WordPress, but the question applies to any CMS).
I need to redirect from links (also dynamic) structured as:
sitename.com/issue/february-2016/post/dynamic-post-name
(february-2016 is an example - could be 'march-2014' or any of a range of terms)
to:
sitename.com/post/dynamic-post-name
Another way to say this: Any request url with /article/ needs to grab that last string (which I think would be the wildcard?) and redirect it as: sitename.com/post/$
Is this possible?
Update: With more research, I found a possible answer that worked in a testing tool, although I've not tested it live on my site.
Does this look correct?
RewriteRule ^([^/]+)/([^/]+)/article/([^.]+)$ article/$3 [QSA,L]
RewriteRule ^article/.*/(.*)$ post/$1 [QSA,L,R=301]
Something like this should work.
The characters captured within the brackets (.*) will be the $1.
Feel free to change article and post to fit your need.
In this case, it will redirect
http://example.com/article/february-2016/post/dynamic-post-name
to
http://example.com/post/dynamic-post-name
I want to rewrite my URL below
https://xxx.xxx/159/prepared-is-me-marianne-pleasure-likewise-debating-wonder?show=199
to become like this
https://xxx.xxx/159/prepared-is-me-marianne-pleasure-likewise-debating-wonder/show/199
My current rewrite is this
RewriteRule ^/(.*)/(.*)?show=(.*)$ /$1/$2/show/$3 [L]
But it does not to work.
as I test here my rule http://htaccess.mwl.be/ it says the my rule doesn't meet the condition of the input URL
I usually use http://www.generateit.net/mod-rewrite/index.php to create my rewriting rules, hope it help !
As explained on this previous question, RewriteRules do not take the querystring into account, only the actual path. So your ?show= part of your original URL will never be matched.
I have a website:
www.mydomain.com/subfolder/subfolder/index.php
How could always hide the directory names from the url. I mean always hide the 2 subfolders' names from the url as the page changes.
Example:
These urls:
www.mydomain.com/subfolder/subfolder/index.php
www.mydomain.com/subfolder/subfolder/about.php
www.mydomain.com/subfolder/subfolder/contact.php
...
Becomes:
www.mydomain.com/index
www.mydomain.com/about
www.mydomain.com/contact
...
I want to use the last mentioned urls for requesting these pages, too, without typing a horrible long url.
you can find some good hints when you will look for mod_rewrite or mod rewrite in htaccess.
If you have access to the Rewrite engine, you can use a simple rewriting pattern similar to this:
RewriteRule ^/(.*)$ /subfolder/subfolder/$1.php
I have the following rewrite rule which is working fine:
RewriteRule ^([a-zA-Z\-]+)[/]?$ apply/?survey_folder=$1
which turns this: address.com/folder1/
into this: address.com/apply/?survey_folder=folder1
The problem is that I can't figure out a way to use a redirect to the rewritten URL. Using cfml as an example, (cflocation url="http://www.address.com/folder1/") throws an error because, of course, the folder "folder1" doesn't actually exist on the server.
I can get the user redirected to the correct page by using /apply/?survey_folder=folder1, but that defeats the purpose of having the rewrite rule at all.
Is there any way to redirect to a URL rewritten by htaccess? I'm new at RewriteRule.
Add the [L] flag to the rule otherwise you're in danger of infinite loops and other problems.
The redirect you need is incredibly simple.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /apply/(index\.php)?\?survey_folder=([\ ]+)\ HTTP/
RewriteRule ^apply/(index\.php)? http://www.example.com/%1 [R=301,L]
RewriteRule ^([A-Z-]+)$ /apply/?survey_folder=$1 [NC,L]
Do not let both URLs with trailing slash and URLs without trailing slash resolve to the same content. The URL for a page does not have a trailing slash. Add another preceding redirect to strip trailing slashes in that case.
Have you tried setting RewriteBase? Its purpose is to map the URL to the physical location on the server. I'm not completely sure this is the problem you are having, but it does sound like a possibility.
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritebase
I need to grab some of my website's old URLs and do a 301 redirect to the new ones, since they are already indexed and we don't want to loose relevance after the change. The old URL is in fact very ugly and for some reason everything I try to do to rewrite it does not work. Here it is:
http://www.mywebsite.com/ExibeCurso.asp?Comando=TreinamentoGeral&codCurso=136&Titulo=Como%20Estruturar%20um%20Sistema%20Gerencial%20de%20Controles%20Organizacionais,13
Basically, I need to translate it into something like:
http://www.mywebsite.com/curso/136
From the old URL I need to check if the user typed "ExibeCurso.asp"; then I know I must send him here: /curso. I must also grab the integer that was in the querystring parameter "codCurso" (136). What is the regular expression I must use for this. I am using ISAPI_Rewrite 3, which basically implements htaccess on IIS, so there should be no difference in terms of syntax. Thanks.
Try this rule:
RewriteCond %{QUERY_STRING} ^([^&]*&)*codCurso=([0-9]+)(&.*)?$
RewriteRule ^/ExibeCurso\.asp$ /curso/%2? [L,R=301]
But I’m not sure whether ISAPI Rewrite requires the pattern to begin with a slash.
Off the top of my head, something like this should work:
RewriteRule ^ExibeCurso.asp(.*)$ http://www.mywebsite.com/curso/$1 [L,R=301]
That would at least send the traffic to /curso/ with all parameters attached. Maybe it's best to process it from there.