Re-write userprofile URL (remove.html) - mod-rewrite

Any Ideas how to change remove .html in this mod-rewrite script
Doesnt work if I remove ".html"
RewriteRule ^([^/]*)\.html$ /userprofile.php?member_id=$1 [L]
Works as
http://site.com/12.html
but wants to have it as
http://site.com/12
Thank you

To make the .html optional, put it in a group and use the ? quantifier:
RewriteRule ^([^/]*)(\.html)?$ /userprofile.php?member_id=$1 [L]
But as this pattern will now also match any single path segment, you should make it more specific to only match your specific URL path pattern. In this case \d+ instead of [^/]* would be a better choice:
RewriteRule ^(\d+)(\.html)?$ /userprofile.php?member_id=$1 [L]

MODIFIED:
I have tested the following and they work on a CentOS server running Apache.
For directory rewriting:
RewriteRule ^/test/(.*)$ http://www.google.com [L,R]
That will redirect http://www.site.com/test to http://www.google.com.
For all files in a directory this will work:
RewriteRule ^/test/([^/]*)(.*)$ http://www.google.com [L,R]
That will redirect www.site.com/test/12.html or www.site.com/test/298.aspx or any other file in the "test" directory to www.google.com.
So this may be more what you are looking for:
RewriteRule ^/12/([^/]*)(.*)$ /userprofile.php?member_id=$1 [L,R]
ORIGINAL POST:
I believe this is what you are looking for:
RewriteRule ^([^/]*)(.*)$ /userprofile.php?member_id=$1 [L]
That is if you are trying to do the rewrite for files...
It will be slightly different if you want to do the rewrite against a directory.

You need to remove the backslash as well ("\.html").

Related

Redirect not working for matching a pattern in my url

I am trying to redirect a pattern of urls
http://style.com/style-blog/entry/what-im-looking-for-in-my-next-15-inch-laptop
to
http://style.com/blog/entry/what-im-looking-for-in-my-next-15-inch-laptop
I have tried to match the "style-blog" here
^style-blog/([A-Za-z0-9-]+)$ or ^style-blog/$
I needed to the first version to get ANY remaining part of the url to append to the new url here
RewriteRule ^style-blog/([A-Za-z0-9-]+)$ http://style.com/you-blog/$1 [NC,L]
Thanks for pointers on what I am doing wrong.
I think you may just be missing a /:
RewriteRule ^/style-(blog/[A-Za-z0-9-]+)$ http://style-review.com/$2
Using wildcard:
RewriteRule ^/style-(blog/.*)$ http://style-review.com/$2
Do you even need the full URL?
RewriteRule ^/style-(blog/.*)$ /$2
Are the ^ and $ required? Wouldn't just this work?
RewriteRule style-blog/ blog/
RewriteRule ^style-blog/(.*)/(.*)$ http://%{HTTP_HOST}/you-blog/$1/$2 [R=301,L]
This appears to be working but thanks to you guys you got me on the right track and probably you didn't have enough information to solve it. Its not fully tested yet and might not cope with any extra 'folders' (bits between slashes)

Rewrite Query String with .htaccess

I'm trying to do a very simple rewrite of a query string
http://www.example.com/library.php?q=abscessed-tooth
to
http://www.example.com/library/abscessed-tooth
This is the code that I've written in my .htaccess file and it is doing nothing
RewriteEngine On
RewriteRule ^/library/?([^/]*)/?\/http://www.example.com/library.php?q=$1 [L]
Maybe likely .htaccess files are not considered in your environment. If in doubt turn on RewriteLogging as it is explained in the excellent documentation of the rewriting module.
Oh, and check the error log, you have a syntax error in the RewriteRule anyway: RewriteRule takes 2 arguments plus flags, your rule has only a single argument:
RewriteEngine On
RewriteRule ^library/([^/]*) http://www.example.com/library.php?q=$1 [L]
You need dollar sign in the end of "left" part not question mark:
^/library/([^/]*)/$ http://www.example.com/library.php?q=$1 [L]
Also do you need the question mark between / and ( ? It doesn't look like lookahead or lookbehind?
Try without wrapping slashes as well
^library/([^/]*)$ http://www.example.com/library.php?q=$1 [L]

.htaccess RewriteRule for change the path if a word appears in the path

:-)
I need a .htaccess RewriteRule that can do this:
If the browser load this url path:
http://www.domain.com/example/word_to_change/
change to
http://www.domain.com/example/new_word/
take a look that the "word_to_change" and "new_word" appears in the third level in the path
I was tried this:
RewriteRule ^(word_to_change/)?$ /new_word/ [R,L]
But only works if the "word_to_change" appears in the second level, not in the third.
thanks for your help! :)
ADDED:
three examples:
need to change
http://www.domain.com/second_level_with_any_word/specific_word_1 or
http://www.domain.com/example_1/specific_word_1 or
http://www.domain.com/example_2/specific_word_1
to
http://www.domain.com/second_level_anything/specific_word_2 or
http://www.domain.com/example_1/specific_word_2 or
http://www.domain.com/example_2/specific_word_2
maybe is mor simple for explain with this other example:
http://*/*/specific_word1
for
http://*/*/specific_word2
This depends on what to acomplish, if you want to change ANY word that apeaers after example with a SPECIFIC word then what you need is:
RewriteRule ^example/(.*) /new_path/ [R,L]
if you want to pass that word as a paramater you can use
RewriteRule ^example/(.*) /new_path/process.php?word=$1 [R,L]
if you want to send any URL that ends with a specific word try this
RewriteRule ^(.*)/old_word/$ /$1/new_word/ [R,L]

Need help removing part of filename from url with mod_rewrite

I'm trying to reformat my url to be a bit shorter. Right now the links end up as this: website.com/image?id=name.jpg
What I want to have the link come out as is m.website.com/name, without the file exension or image.php file in the url. I figure mod_rewrite is the way to do it, so any help will be greatly appreciated.
In order to make it so someone accessing the URL http://m.website.com/name gets served the content for http://website.com/image?id=name.jpg, you first need to check the hostname for m.website.com, then match the name part of the URI. Using that match, you can proxy the request (using a [P]) or, if both website.com and m.website.com are hosted on the same server, just simply internally rewrite. Try putting this in your .htaccess file in your document root:
RewriteEngine on
# check the host (NC = no case)
RewriteCond %{HTTP_HOST} ^m\.website\.com$ [NC]
# don't rewrite /image
RewriteCond %{REQUEST_URI} !^/image
# Match the first non-slash word and rewrite
RewriteRule ^([^/]+)$ /image?id=$1 [L]
This will rewrite http://m.website.com/name to /image?id=name.jpg, but it will not rewrite http://m.website.com/path/name. If you want paths (and everything else) to be included in the id parameter, change the ([^/]+) to (.*) in the RewriteRule.

rewriting URLs from .json to .php

I want http://server/path/app.json?a=foo&b=bar to map to http://server/path/foo.php?a=foo&b=bar using mod_rewrite. I have the following incantation in my .htaccess which doesn't give any joy
RewriteEngine On
RewriteRule ^([^.?]+).json(.*)$ $1.php$2 [L]
Suggestions?
Update: Adding rewrite.log and error.log output (comments don't allow formatting)
I get the following in the rewrite.log
strip per-dir prefix: /Users/user/Sites/dir/app.json -> app.json
applying pattern '^([^.?]+).json(.*)$' to uri 'app.json'
rewrite 'app.json' -> 'app.php'
add per-dir prefix: app.php -> /Users/user/Sites/dir/app.php
internal redirect with /Users/user/Sites/dir/app.php [INTERNAL REDIRECT]
and the apache server log says
The requested URL /Users/user/Sites/dir/app.php was not found on this server.
If I read your question correctly you want:
http://server/path/app.json?a=foo&b=bar
Going to:
http://server/path/foo.php?a=foo&b=bar
Sowhen you capture (app).json $1 is app and $2 is your second parenthesis, it's ... nothing (the part between json and the ?). As everything after the question mark is the QUERY STRING and cannot be captured here. Your rewriteRule is working on the requested file, not on the QUERY STRING. So you didn't captured foo anywhere. For the QUERY_STRING you could use the [QSA] flag on the rewriteRule, that would simply append a=foo&b=bar after your rewrite.
RewriteEngine On
RewriteRule ^([^.?]+).json(.*)$ $1.php$2 [L]
Here you tell apache to reuse $1 (the filename without .json), so app.json will get redirected to app.php, not foo.php.
RewriteEngine On
RewriteRule ^([^.?]+).json(.*)$ $1.php [L,QSA]
Will redirect app.json?a=b&z=r to app.php?a=b&z=r.
Now if you really need to capture foo as the first QUERY_STRING parameter the rule will become harder. But you could do it like that (here instead of the first parameter I detect the parameter 'a=' and capture his value in %4):
RewriteCond %{QUERY_STRING} (.*)(^|&|%26|%20)a(=|%3D)([^&]+)(.*)$
RewriteRule ^([^.?]+).json$ %4.php? [L,QSA]

Resources