file1.xml file2.xml in htaccess - .htaccess

I have a website in php.
I generate my xml files like this:
file1.xml
file2.xml
file3.xml
...
myhtaccess
RewriteRule ^file\.xml$ file.php [L]
so when I type file.xml it opens file.php.
Is there any way to do something like this:
file5.xml -> file.php?n=5
I want to get the number after file file7.xml and send it to php like file.php?n=7

Try :
RewriteRule ^file([0-9]+)\.xml$ file.php?n=$1 [L]
This will internally redirect /fileNumber.xml to /file.php?n=number .

Related

.htaccess regex redirect avoid by file type

I have two URL conditions and I wanted to redirect them like this:
https://www.example.com/feeds/4aceXy to https://www.example.com/direct_feed/4aceXy
Now the problem is, I am also using the URL for an older link like this one:
https://wwww.example.com/feeds/5bdb39711b41d479273e678a6f356603d7109ffc.xml
I wanted to avoid redirect with .xml extension here is my current redirect:
RewriteRule feeds/(.*)?$ https://wwww.example.com/direct_feed/$1 [QSA,L]
It works fine but I don't want to redirect it with .xml based URL.
My question is - is there a condition that can help me to avoid the rewrite if a parameter contains .xml in regX (.*)?$
You can use a negative lookbehind:
RewriteRule feeds/(.*)?(?<!\.xml)$ https://wwww.example.com/direct_feed/$1 [QSA,L]

.htaccess how to create an URLs like http://example.com/~someuser

How do I can create and process an URLs like this via .htaccess rules: http://example.com/~someuser
And then get a parameter "username" as a part of $_GET array. Thanks in advance.
Something like this:
RewriteEngine On
RewriteRule ^~([^/]+)/?$ /script.php?user=$1 [L]
If those rules are placed in the htaccess file in your document root, then it'll capture the "someuser" part of the URL and rewrite it to the script.php script and set it as the $_GET['user'] variable. You can replace script.php with whatever name of the script you want the variable to go to.

want to replace .index.php?backLinks_free_Backlinks=1&totalRows_free_Backlinks=7 part by /backLinks_free_Backlinks/1 from my url using .htaccess

i have a link look like this
http://backlinks.cheapratedomain.com/index.php?backLinks_free_Backlinks=1&totalRows_free_Backlinks=7
want to replace .index.php?backLinks_free_Backlinks=1&totalRows_free_Backlinks=7 this part by /backLinks_free_Backlinks/1 using .htaccess
after replacing link will be look like this
http://backlinks.cheapratedomain.com/backLinks_free_Backlinks/1
Try adding this to your htaccess file in the document root:
RewriteEngine On
RewriteRule ^backLinks_free_Backlinks/([0-9]+)$ /index.php?backLinks_free_Backlinks=$1&totalRows_free_Backlinks=7 [L,QSA]
You can remove the &totalRows_free_Backlinks=7 bit from the target if you don't need it.

.htaccess issue with redirecting folder

I have a folder named /test in my application.
Right now i am trying to write an .htaccess file that would show all requests to /test* as /test.
For example:
www.example.com/test/ is the actual directory with index.php file in it.
All the requests like the following should go to the same /test directory
www.example.com/test-hello/
www.example.com/test-world/
www.example.com/test-htacess/
www.example.com/test123/
Basically any requests to /test* should go to /test.
This is what I've tried so far:
RewriteRule ^/test* /test
You need to use RewriteCond to first match "test in url"
Try below:
RewriteCond %{THE_REQUEST} ^GET\ /test/
RewriteRule ^test/(.*) /test/$1 [L,R=301]
Your regular expression is wrong. You mean ^/test.*$. Your rule would match to /testtttt.
The asterisk means that the char in front of it can be zero or more times included. The dot is a special char which means here could be anything. the .* matches every string including an empty string. See also Wikipedia.
You currently are not putting the -hello, -world etc behind your folder. What is hello? Is that the file? Or the param?
The second part of the rewriteRule should be a file. Something like
RewriteRule ^/test(.*)$ /test/$1.php
Above function will have:
/testABC to /test/ABC.php
But I don't understand what you want to accomplish?

.htaccess modification

I am using direct paths for downloading files from my site. the link is something like this
http://www.site.com/download.php?dir1/dir/dir3/file.doc
i want to wrap it with mod rewite rules so that only below link should be appeared
http://www.site.com/download
file, dir and dir3 are variable.
what i'hv to do in my .htaccess file?? Any Idea??
A simple redirect would be:
RewriteRule ^http://www.site.com/download/(.*)/?$ http://www.site.com/download.php?dir1/dir/dir3/$1 [NC,L]
This will take any request for something in the 'artificial' download directory and route it to the real location.
You can add more complex rules stripping out filetypes etc depending on your needs, or redirecting a 'name' to a filename etc etc..
e.g:
RewriteRule ^http://www.site.com/download/pdf/(.*)/?$ http://www.site.com/download.php?dir1/dir/dir3/$1.pdf [L,NC]
This would have an artificial PDF folder containing a filename ex the extension, routing to a .pdf doc....you can shape the redirect any way you like really...depends on the format you prefer
Not specific question. What is dir1/dir/dir3/file.doc means? If you want to get http://www.site.com/download.php?dir1/dir/dir3/file.doc, when you go to http://www.site.com/download do the next things in your .htaccess file.
RewriteEngine on
RewriteRule ^download/(.*)/?$ download.php?dir1/dir/dir3/file.doc [L]

Resources